﻿namespace Vdc.Grid
{
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class GridDataControlFieldCell : DataControlFieldCell
    {
        public GridDataControlFieldCell(DataControlField containingField) : base(containingField)
        {
        }

        public override Control FindControl(string id)
        {
            if (this.Controls[0].Controls[0].Controls[0].Controls.Count == 0)
            {
                this.Controls[0].Controls[0].Controls[0].DataBind();
            }
            return this.Controls[0].Controls[0].Controls[0].Controls[0].FindControl(id);
        }

        public virtual ArrayList FindControlsByType(Type type)
        {
            return this.FindControlsByType(type, false);
        }

        public virtual ArrayList FindControlsByType(Type type, bool searchInTargetControls)
        {
            Control container = null;
            ArrayList controls = new ArrayList();
            if (this.Controls[0].Controls[0].Controls[0].Controls.Count == 0)
            {
                this.Controls[0].Controls[0].Controls[0].DataBind();
            }
            container = this.Controls[0].Controls[0].Controls[0].Controls[0];
            this.GetControlsByType(container, type, controls, searchInTargetControls);
            return controls;
        }

        protected virtual void GetControlsByType(Control container, Type type, ArrayList controls, bool searchInTargetControls)
        {
            foreach (Control control in container.Controls)
            {
                if (control.GetType() == type)
                {
                    controls.Add(control);
                }
                if (searchInTargetControls || (control.GetType() != type))
                {
                    this.GetControlsByType(control, type, controls, searchInTargetControls);
                }
            }
        }

        internal virtual string GetInnerText()
        {
            if (((this.Controls.Count > 0) && (this.Controls[0].Controls.Count > 0)) && (this.Controls[0].Controls[0] is HtmlContainer))
            {
                return ((HtmlContainer) this.Controls[0].Controls[0]).Text;
            }
            return base.Text;
        }

        public override string Text
        {
            get
            {
                return this.Value;
            }
            set
            {
                if (((this.Controls.Count > 0) && (this.Controls[0].Controls.Count > 0)) && (this.Controls[0].Controls[0] is HtmlContainer))
                {
                    ((HtmlContainer) this.Controls[0].Controls[0]).Text = value;
                }
                else
                {
                    base.Text = value;
                }
            }
        }

        public string Value
        {
            get
            {
                if (((this.Controls.Count > 0) && (this.Controls[0].Controls.Count > 0)) && (this.Controls[0].Controls[this.Controls[0].Controls.Count - 1] is HtmlContainer))
                {
                    return ((HtmlContainer) this.Controls[0].Controls[this.Controls[0].Controls.Count - 1]).Text;
                }
                return base.Text;
            }
            set
            {
                if (((this.Controls.Count > 0) && (this.Controls[0].Controls.Count > 0)) && (this.Controls[0].Controls[this.Controls[0].Controls.Count - 1] is HtmlContainer))
                {
                    ((HtmlContainer) this.Controls[0].Controls[this.Controls[0].Controls.Count - 1]).Text = value;
                }
                else
                {
                    base.Text = value;
                }
            }
        }
    }
}

